#ifndef __VIDEO_ENCODE_H__
#define __VIDEO_ENCODE_H__

#include <map>
#include <mutex>
#include <functional>
#include "Pal/VideoEnc.h"
#include "frame_common.h"
#include "Thread/Thread.h"
#include "json.h"
#include "ConfigApi.h"
#include "mediaPriv.h"

namespace Media{

class VideoEncode{
public:
	//using CallBack = std::function<void(const FrameCommon::Frame::Ptr &)>;
	static VideoEncode *instance(int channel);	

	bool getFormat(VideoEncFormat *format);

	bool setFormat(VideoEncFormat* format);

	bool setFormat(const Json::Value &table);

	bool setIFrame();

	bool start(void *thiz, const FrameCommon::frame_callback_func& callback);

	bool stop(void *thiz);

	bool getPacket(SCREEN_vencPacket_t *pPacket);

	bool releasePacket();

	bool start();

	bool stop();
	
private:
	VideoEncode(int channel);

	bool createEncoder();

	bool Init();

	void parseConfig(const Json::Value& config, VideoEncFormat& format);

	bool onConfig(const Json::Value &newTable, const Json::Value &oldTable = Json::Value::null, int &result = ConfigApi::IConfigInterface::result_null);

	bool onConfigVerify(const Json::Value &newTable, const Json::Value &oldTable = Json::Value::null, int &result = ConfigApi::IConfigInterface::result_null);

	void ThreadProc(Fdt::Thread *thread);
private:
	static std::map<int, VideoEncode *> 	s_encoder_map;


	std::string								_configName{"Encode.Video.Chn"};
	Json::Value								_config;
	bool									_started = false;
	VideoEncFormat							_format;
	bool									_setting = false;
	VideoEncoder *							_videoEncoder = nullptr;
	int 									_channel = 0;
	std::recursive_mutex 					_mutex;
	std::map<void *, std::shared_ptr<FrameCommon::frame_callback_func>> 	_func_map;
	VideoEncPacket                          m_encPacket;

	int _width = 1920;
	int _height = 1080;
	Fdt::Thread 						_thread{"Video0"};
};

}//namespace Media
#endif


